#drafted by Jeremy VanDerWal ( jjvanderwal@gmail.com ... www.jjvanderwal.com )
#GNU General Public License .. feel free to use / distribute ... no warranties

################################################################################
###start doing the work
library(climates); library(SDMTools) #define the libraries needed

wd = '~/Climate/CIAS/Australia/5km/tmp.pbs/';setwd(wd) #define and set the working directory
fd = '~/Climate/CIAS/global.20120322/' #define the locations of the future data

ESs = list.files(paste(fd,'monthly_csv/',sep='')); ESs = ESs[-grep('current',ESs)] #get a list of emission scenarios
GCMs = list.files(paste(fd,'monthly_csv/',ESs[1],sep='')) #get a list of the GCMs

for (es in ESs) { cat(es,'\n') #cycle through each emission scenario
	for (gcm in GCMs) { 
		##create the sh file
		zz = file(paste(es,'_',gcm,'.sh',sep=''),'w')
			cat('#!/bin/bash\n',file=zz)
			cat('cd $PBS_O_WORKDIR\n',file=zz)
			cat('module load java\n',file=zz)
			cat("R CMD BATCH '--args es=",'"',es,'" gcm="',gcm,'" ',"' ",'/home/jc165798/SCRIPTS/sdmcode/climate/CIAS/10.script2run.R ',es,'_',gcm,'.Rout --no-save \n',sep='',file=zz)
		close(zz)
				
		#submit the job
		system(paste('qsub -m n -l nodes=1:ppn=3 ',es,'_',gcm,'.sh',sep=''))		
	}
}